<?php
/**
 * Created by PhpStorm.
 * User: mojie126
 * Date: 2017/5/24 0024
 * Time: 上午 9:30
 */

namespace app\common;

use Firebase\JWT\JWT;

class verify
{
	static public function encode($dejwt, $exptime = NULL, $val = [])
	{
		$token = [
			"iss" => APP_NAME,
			"jti" => request()->token(),
			"aud" => getWebURL(),
			"sub" => $dejwt,
			"iat" => time(),
			"nbf" => time()
		];
		if (!empty($exptime)) {
			$token = array_merge($token, ["exp" => time() + $exptime]);
		}
		if (!empty($val)) {
			foreach ($val as $k => $v) {
				$token = array_merge($token, [$k => $v]);
			}
		}

		return JWT::encode($token, KEY, "HS512");
	}

	static public function decode($jwt)
	{
		try {
			$res = JWT::decode($jwt, KEY, array("HS512"));
		} catch (\Exception $exception) {
			return FALSE;
		}

		return $res;
	}
}